//
//  KKHotChooseToolBar.swift
//  eyepetizer
//
//  Created by 柯平常 on 16/6/5.
//  Copyright © 2016年 stone. All rights reserved.
//

import UIKit
import SnapKit

private let baseTag = 1000
private let btnHeight = 44
private let buttonCount: CGFloat = 4



class KKOrderChooseToolBar: UIView {
    
    
    var lintViewCenterXCons: Constraint?
    
    //定义属性 记录选中的按钮
    var currentBtn: UIButton?
    
    //定义闭包
    var selectedIndexCallBack: ((_ index: Int) -> ())?
    
    
    init() {
        super.init(frame: CGRect.zero)
        
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        let waitSureButton = addChildButtons(title: "Pending", index: 0)
        let isProcessButton = addChildButtons(title: "In Process", index: 1)
        let waitPayButton = addChildButtons(title: "Payment", index: 2)
        let completedButton = addChildButtons(title: "Completed", index: 3)
        
        addSubview(lineView)
        addSubview(blueView)
        
        
        
        
        waitSureButton.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH/buttonCount, height: 40)
        isProcessButton.frame = CGRect(x: SCREEN_WIDTH/buttonCount, y: 0, width: SCREEN_WIDTH/buttonCount, height: 40)
        waitPayButton.frame = CGRect(x: SCREEN_WIDTH * 2/buttonCount, y: 0, width: SCREEN_WIDTH/buttonCount, height: 40)
        completedButton.frame = CGRect(x: SCREEN_WIDTH * 3/buttonCount, y: 0, width: SCREEN_WIDTH/buttonCount, height: 40)
        
        
        blueView.frame = CGRect(x: waitSureButton.x + baseMargin, y: 37, width: SCREEN_WIDTH/buttonCount - baseMargin*2, height: 3)
        lineView.frame = CGRect(x: 0, y: 39, width: SCREEN_WIDTH, height: SCREEN_SCALE)
    }
    
    lazy var blueView: UIView = {
        let blueView = UIView()
        blueView.backgroundColor = UIColor.colorWithHexString(hex: "#05a2e8")
        return blueView
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.colorWithHexString(hex: "e6e6e6")
        return view
    }()
    
    private func addChildButtons(title: String, index: Int)->UIButton {
        let btn = UIButton()
        //设置按钮的tag
        btn.tag = index + baseTag
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(COLOR_NAV_ITEM_NORMAL, for: .normal)
        btn.titleLabel?.font = baseFont(size: 15)
        //设置按钮的点击事件
        btn.addTarget(self, action: #selector(btnDidClick(btn:)), for: .touchUpInside)
        btn.setTitleColor(UIColor.colorWithHexString(hex: "#03a3ea"), for: .selected)
        addSubview(btn)
        if index == 0 {
            btn.isSelected = true
            //记录选中的按钮
            currentBtn = btn
        }
        return btn
    }
    
    @objc private func btnDidClick(btn: UIButton) {
        //取消之前按钮的选中状态
        currentBtn?.isSelected = false
        btn.isSelected = true
        currentBtn = btn
        UIView.animate(withDuration: 0.1) {
            self.blueView.x = btn.x + baseMargin
        }
        selectedIndexCallBack!(btn.tag - baseTag)
    }
    
    
    func setSelectedBtn(section: Int) {
        //取消之前按钮的选中状态
        //根据section 获取对应的按钮
        //如果tag为 0 取到的是对象的自己
        let btntag = baseTag + section
        let btn = self.viewWithTag(btntag) as! UIButton
        currentBtn?.isSelected = false
        btn.isSelected = true
        UIView.animate(withDuration: 0.1) {
            self.blueView.x = btn.x + baseMargin
        }
        currentBtn = btn
    }
    
}
